# -*-coding=utf-8-*-

import numpy as np


class Light():
    def __init__(self):
        self.radius = 3.0
        self.position = np.array([0.0, 50.0, -20.0], 'f')
        self.color = np.array([255, 255, 255], 'H')
        self.mode = 'light'

    def intersect(self, ray):
        """
        求交点
        """
        # 球心到射线的距离
        center_to_ray = np.abs(np.linalg.norm(np.cross(self.position - ray.position, ray.direction)) / np.linalg.norm(ray.direction))
        # 射线和球面相交时
        if center_to_ray <= self.radius:
            # 射线起点到垂点距离
            length_1 = np.sqrt(np.dot(self.position - ray.position, self.position - ray.position) - center_to_ray ** 2)
            # 交点到垂点距离
            length_2 = np.sqrt(self.radius ** 2 - center_to_ray ** 2)
            # 射线到交点距离比例值
            t = length_1 - length_2
            return (t, self.color)
        else:
            return (None, None)
